package com.sctvcloud.linshui.ui.adapter.holder;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ruihang.generalibrary.ui.adapter.holder.BaseRecylerHolder;
import com.ruihang.generalibrary.ui.widget.CustomEXImageView;
import com.ruihang.generalibrary.utils.DateUtils;
import com.sctvcloud.linshui.R;
import com.sctvcloud.linshui.beans.GalleryBean;
import com.sctvcloud.linshui.ui.utils.GlideUtil;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/7/17 0017.
 */

public class GalleryHolder extends BaseRecylerHolder<GalleryBean> implements View.OnClickListener {

    @BindView(R.id.li_1)
    protected LinearLayout li_1;
    @BindView(R.id.li_2)
    protected LinearLayout li_2;
    @BindView(R.id.li_3)
    protected LinearLayout li_3;

    @BindView(R.id.content1)
    protected TextView content1;
    @BindView(R.id.content2)
    protected TextView content2;
    @BindView(R.id.content3)
    protected TextView content3;
    @BindView(R.id.img1)
    protected CustomEXImageView img1;
    @BindView(R.id.img2)
    protected CustomEXImageView img2;
    @BindView(R.id.img3)
    protected CustomEXImageView img3;
    @BindView(R.id.img4)
    protected CustomEXImageView img4;
    @BindView(R.id.img5)
    protected CustomEXImageView img5;
    @BindView(R.id.pic_count)
    protected TextView pic_count;
    @BindView(R.id.pic_count_2)
    protected TextView pic_count_2;
    @BindView(R.id.pic_count_3)
    protected TextView pic_count_3;

    @BindView(R.id.media1)
    protected TextView media1;
    @BindView(R.id.media2)
    protected TextView media2;
    @BindView(R.id.media3)
    protected TextView media3;
    @BindView(R.id.time1)
    protected TextView time1;
    @BindView(R.id.time2)
    protected TextView time2;
    @BindView(R.id.time3)
    protected TextView time3;
    private GalleryBean item;
    public GalleryHolder(Context context, View itemView) {
        super(context, itemView);
        itemView.setOnClickListener(this);
        li_1.setOnClickListener(this);
        li_2.setOnClickListener(this);
        li_3.setOnClickListener(this);
    }

    @Override
    protected void initInject() {
        ButterKnife.bind(this, itemView);
    }

    @Override
    public void setData(GalleryBean item) {
        this.item = item;

       // 1.单图大封面，2.单图小封面，3.三图封面
        if(item.getNewsCoverType()==1){
            li_1.setVisibility(View.GONE);
            li_2.setVisibility(View.VISIBLE);
            li_3.setVisibility(View.GONE);
            if(!TextUtils.isEmpty(item.getNewsTitle())) {
                content2.setText(item.getNewsTitle());
            }
            if(!TextUtils.isEmpty(item.getNewsPicCount())) {
                pic_count.setText(item.getNewsPicCount()+"张");
                pic_count.setVisibility(View.VISIBLE);
            }else{
                pic_count.setVisibility(View.GONE);
            }

            if (!TextUtils.isEmpty(item.getNewsCover())) {
                GlideUtil.getGlideWithLarge169Def(context, item.getNewsCover()).into(img4);
            }
            if(!TextUtils.isEmpty(item.getNewsSource())){
                media2.setText(item.getNewsSource());
            }
            time2.setText(!TextUtils.isEmpty(item.getPubTime()) ? DateUtils.formatDate(DateUtils.getDataDate(item),"") : "");
        }
        else if(item.getNewsCoverType()==2){
            li_1.setVisibility(View.GONE);
            li_2.setVisibility(View.GONE);
            li_3.setVisibility(View.VISIBLE);

            if(!TextUtils.isEmpty(item.getNewsTitle())) {
                content3.setText(item.getNewsTitle());
            }
            if (!TextUtils.isEmpty(item.getNewsCover())) {
                GlideUtil.getGlideWith11Def(context, item.getNewsCover()).centerCrop().into(img5);
            }

            if(!TextUtils.isEmpty(item.getNewsSource())){
                media3.setText(item.getNewsSource());
            }
            time3.setText(!TextUtils.isEmpty(item.getPubTime()) ? DateUtils.formatDate(DateUtils.getDataDate(item),"MM-dd") : "");

            if(!TextUtils.isEmpty(item.getNewsPicCount())) {
                pic_count_2.setText(item.getNewsPicCount()+"张");
                pic_count_2.setVisibility(View.VISIBLE);
            }else{
                pic_count_2.setVisibility(View.GONE);
            }

        }
        else if(item.getNewsCoverType()==3){
            li_1.setVisibility(View.VISIBLE);
            li_2.setVisibility(View.GONE);
            li_3.setVisibility(View.GONE);

            if(!TextUtils.isEmpty(item.getNewsTitle())) {
                content1.setText(item.getNewsTitle());
            }

            if (!TextUtils.isEmpty(item.getNewsCover())) {
                String[] imgs = splitImgs(item.getNewsCover());
                if(imgs.length==1){
                    GlideUtil.getGlideWith11Def(context, imgs[0]).centerCrop().into(img1);
                    img1.setVisibility(View.VISIBLE);
                    img2.setVisibility(View.GONE);
                    img3.setVisibility(View.GONE);
                }else  if(imgs.length==2){
                    GlideUtil.getGlideWith11Def(context, imgs[0]).centerCrop().into(img1);
                    GlideUtil.getGlideWith11Def(context, imgs[1]).centerCrop().into(img2);
                    img1.setVisibility(View.VISIBLE);
                    img2.setVisibility(View.VISIBLE);
                    img3.setVisibility(View.GONE);
                }else if(imgs.length==3){
                    GlideUtil.getGlideWith11Def(context, imgs[0]).centerCrop().into(img1);
                    GlideUtil.getGlideWith11Def(context, imgs[1]).centerCrop().into(img2);
                    GlideUtil.getGlideWith11Def(context, imgs[2]).centerCrop().into(img3);
                    img1.setVisibility(View.VISIBLE);
                    img2.setVisibility(View.VISIBLE);
                    img3.setVisibility(View.VISIBLE);
                }else{
                    img1.setVisibility(View.GONE);
                    img2.setVisibility(View.GONE);
                    img3.setVisibility(View.GONE);
                }

            }

            if(!TextUtils.isEmpty(item.getNewsPicCount())) {
                pic_count_3.setText(item.getNewsPicCount()+"张");
                pic_count_3.setVisibility(View.VISIBLE);
            }else{
                pic_count_3.setVisibility(View.GONE);
            }

            if(!TextUtils.isEmpty(item.getNewsSource())){
                media1.setText(item.getNewsSource());
            }
            time1.setText(!TextUtils.isEmpty(item.getPubTime()) ? DateUtils.formatDate(DateUtils.getDataDate(item),"MM-dd") : "");
        }
        else{

        }

    }

    private String[] splitImgs(String org){
       return org.split(",");
    }

    @Override
    public void onClick(View v) {
         if(v.getId() == R.id.li_1){
             if (internalClick != null) {
                 li_1.setTag(R.id.adapter_item_data_key, item);
                 internalClick.onItemInternalClick(li_1, v, getAdapterPosition());
             }
         }else if(v.getId() == R.id.li_2){
             if (internalClick != null) {
                 li_2.setTag(R.id.adapter_item_data_key, item);
                 internalClick.onItemInternalClick(li_2, v, getAdapterPosition());
             }
         }else if(v.getId() == R.id.li_3){
            if (internalClick != null) {
                li_3.setTag(R.id.adapter_item_data_key, item);
                internalClick.onItemInternalClick(li_3, v, getAdapterPosition());
            }
        }

    }

}
